package com.shangma.utils;

import com.shangma.entity.User;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public class RestTemplateTest {
    public static void main(String[] args) {
        // ctrl + alt + m  把代码抽取到方法中
        // put delete 没有返回值?  要操作请求头怎么办?
        RestTemplate restTemplate = new RestTemplate();
        String path = "http://localhost:8080/test/delete/{0}";

        // 创建请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("token","fsdagfdsafKJHKLHYJKIdzfgdGHKJLKJHG");
        // 封装成请求对象
        HttpEntity httpEntity = new HttpEntity(httpHeaders);
        ResponseEntity<String> exchange = restTemplate.exchange(path, HttpMethod.DELETE, httpEntity, String.class, "1,2");
        System.out.println(exchange);
    }

    private static void deleteMethod() {
        // 创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        String path = "http://localhost:8080/test/delete/{0}";
        restTemplate.delete(path,"1,2,3,4,5");
    }

    private static void putRequest() {
        // 创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        String path = "http://localhost:8080/test/update/{0}/{1}";
//        Map<String,Object> map = new HashMap<>();
//        map.put("age",20);
//        map.put("name","张三");
        restTemplate.put(path,null,"12312432142","67657");
    }

    private static void postRequest() {
        // 创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        String path = "http://localhost:8080/test/regist";
        // 创建参数对象 (可以是自定义对象，也可以是map  只要能够转换成json格式即可)
        User user = new User();
        user.setAge(18);
        user.setName("幼年舔狗");

        // 发送post请求 获取响应体数据
//        String s = restTemplate.postForObject(path, user, String.class);
        // 获取所有的响应数据
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(path, user, String.class);
        System.out.println(stringResponseEntity);
//        System.out.println(s);
    }

    private static void getEntity() {
        // 创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        // 获取响应全部信息
        String path = "http://api.tianapi.com/tiangou/index?key=7c4e55e6efd90f0baa513c07bcbffbfb";
        // 获取响应的所有信息
        ResponseEntity<String> forEntity = restTemplate.getForEntity(path, String.class);
        System.out.println(forEntity);
        // 获取响应状态码
        HttpStatus statusCode = forEntity.getStatusCode();
        System.out.println(statusCode);
        // 获取响应头
        HttpHeaders headers = forEntity.getHeaders();
        System.out.println(headers);
        // 获取响应体
        String body = forEntity.getBody();
        System.out.println(body);
    }


    private static void getObject() {
        // 创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        // 简单操作
        String path = "http://api.tianapi.com/tiangou/index?key=7c4e55e6efd90f0baa513c07bcbffbfb";
        // 使用get请求 请求  获取的是响应体中的数据
        String forObject = restTemplate.getForObject(path, String.class);
        System.out.println(forObject);
    }


}
